﻿using System;
using DataAccess;
using System.Data;
using System.Text;

namespace Business
{
    public class BuReport
    {
        public static DataTable selReportDetailed(string sql)
        {
            DaReport report = new DaReport();
            //return report.selReportDetailed(sql);

            //DaProject daProject = new DaProject();
            DataTable tmpProject = report.selReportDetailed(sql);
            int numOfRow = tmpProject.Rows.Count;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    int minute = Int32.Parse(tmpProject.Rows[i]["Minute"].ToString());
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour * 60;
                        tmpProject.Rows[i]["Minute"] = minute;
                        tmpProject.Rows[i]["Hour"] = Int32.Parse(tmpProject.Rows[i]["Hour"].ToString()) + addHour;
                    }
                }
            }
            return tmpProject;
        }

        public static DataTable selReportDetailedOri(string sql)
        {
            DaReport report = new DaReport();
            return report.selReportDetailed(sql);
        }
        public static string selReportUser(string sql, string companyid)
        {
            string result = String.Empty;
            DaReport report = new DaReport();
            
            result += "<table><tr><th>Họ tên</th><th>Thời gian thực tế</th></tr>";

            DataTable listUser = BuStaff.selAllStaffOpt(companyid);
            int numOfUser = listUser.Rows.Count;
            for (int i = 0; i < numOfUser; i++)
            {
                result += "<tr><td>"+listUser.Rows[i]["StaffName"].ToString()+"</td>";
                result += "</tr>";
            }
            
            result += "</table>";
            return result;
        }


        public static string repTimeTotal(int month, int year, bool id, int code, bool isChild, string companyid)
        {
            string result = String.Empty;
            string label = id ? "tháng" : "năm";
            result += @"<table><tr>
			               <th rowspan='2' align='center' style='color:#375B91;background-color:#F3F9FF;width:29px;border: 1px solid #D9E6F0;'>TT</th>
                           <th rowspan='2' align='center' style='color:#375B91;background-color:#F3F9FF;width:240px;border: 1px solid #D9E6F0;'>Tên sản phẩm</th>
                           <th rowspan='2' align='center' style='color:#375B91;background-color:#F3F9FF;width:60px;border: 1px solid #D9E6F0;'>Mã sản phẩm</th>
                           <th rowspan='2' align='center' style='color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #D9E6F0;'>Thời gian sử dụng trong " + label + "</th>";
            result += @"<th colspan='3' align='center' style='color:#375B91;background-color:#F3F9FF;border: 1px solid #D9E6F0;'>Phân loại theo thời gian</th>
                        </tr>
                        <tr>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:110px;border: 1px solid #D9E6F0;'>Giờ tiêu chuẩn</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:120px;border: 1px solid #D9E6F0;'>Ngoài giờ</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:90px;border: 1px solid #D9E6F0;'>Ngày nghỉ(cuối tuần, lễ, tết)</th>
		                </tr>";

            // Lay toan bo du lieu trong bao cao
            DaReport report = new DaReport();
            DataTable data;
            if (id)
            {
                data = report.repTimeTotal(month, year, 1, companyid);
            }
            else
            {
                data = report.repTimeTotal(month, year, 0, companyid);
            }

            // Dua project ra cac arraylist
            DataTable code1 = data.Clone();
            DataTable code2 = data.Clone();
            DataTable code3 = data.Clone();
            string tmp;

            // Luu tru tong gia tri dong tong so
            int sumH0 = 0, sumM0 = 0,sumH1 = 0, sumM1 = 0, sumH2 = 0, sumM2 = 0, sumH3 = 0, sumM3 = 0, sumH10 = 0, sumM10 = 0, sumH11 = 0, sumM11 = 0, sumH12 = 0, sumM12 = 0, sumH13 = 0, sumM13 = 0, sumH20 = 0, sumM20 = 0, sumH21 = 0, sumM21 = 0, sumH22 = 0, sumM22 = 0, sumH23 = 0, sumM23 = 0;

            // Luu tru dong code1, code 2, code 3
            string strCode1 = String.Empty;
            string strCode2 = String.Empty;
            string strCode3 = String.Empty;
            foreach (DataRow item in data.Rows)
            {
                // Loai bo dong co gia tri null
                if (!item["ProjectId"].ToString().Equals(""))
                {
                    tmp = item["ProjectLevel"].ToString();
                    if (tmp.Equals("1"))
                    {
                        code1.ImportRow(item);
                    }
                    if (tmp.Equals("2"))
                    {
                        code2.ImportRow(item);
                    }
                    if (tmp.Equals("3"))
                    {
                        code3.ImportRow(item);
                    }
                }
            }

            int hour, minute, addHour;

            int count = 1;

            foreach (DataRow item1 in code1.Rows)
            {
                if (code == 1)
                {
                    strCode1 += "<tr><td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + (count++) + "</td><td style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'><strong>" + item1["ProjectName"].ToString() + "</strong></td><td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + item1["ProjectCode"].ToString() + "</td>";
                }
                strCode2 = String.Empty;
                sumH10 = 0; sumM10 = 0; sumH11 = 0; sumM11 = 0; sumH12 = 0; sumM12 = 0; sumH13 = 0; sumM13 = 0;
                foreach (DataRow item2 in code2.Rows)
                {
                    if (item2["ProjectParentID"].ToString().Equals(item1["ProjectID"].ToString()))
                    {
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<tr><td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + (count++) + "</td><td style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'><strong><i>" + item2["ProjectName"].ToString() + "</i></strong></td><td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + item2["ProjectCode"].ToString() + "</td>";
                        }

                        strCode3 = String.Empty;
                        sumH20 = 0; sumM20 = 0; sumH21 = 0; sumM21 = 0; sumH22 = 0; sumM22 = 0; sumH23 = 0; sumM23 = 0;
                        foreach (DataRow item3 in code3.Rows)
                        {
                            
                            if (item3["ProjectParentID"].ToString().Equals(item2["ProjectID"].ToString()))
                            {
                                // Thoi gian su dung
                                hour = Int32.Parse(item3["TimeHour"].ToString());
                                minute = Int32.Parse(item3["TimeMinute"].ToString());
                                sumH20 += hour;
                                sumM20 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<tr><td align='center' style='border: 1px solid #D9E6F0;'>" + (count++) + "</td><td style='border: 1px solid #D9E6F0;'>" + item3["ProjectName"].ToString() + "</td><td align='center' style='border: 1px solid #D9E6F0;'>" + item3["ProjectCode"].ToString() + "</td><td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td>";
                                }
                                // Gio tieu chuan
                                hour = Int32.Parse(item3["FirstTimeH"].ToString());
                                minute = Int32.Parse(item3["FirstTimeM"].ToString());
                                sumH21 += hour;
                                sumM21 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td>";
                                }
                                // Ngoai gio
                                hour = Int32.Parse(item3["SecondTimeH"].ToString());
                                minute = Int32.Parse(item3["SecondTimeM"].ToString());
                                sumH22 += hour;
                                sumM22 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td>";
                                }
                                // Ngay nghi
                                hour = Int32.Parse(item3["ThirdTimeH"].ToString());
                                minute = Int32.Parse(item3["ThirdTimeM"].ToString());
                                sumH23 += hour;
                                sumM23 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td></tr>";
                                }
                            }
                        }

                        if (sumM20 >= 60)
                        {
                            addHour = sumM20 / 60;
                            sumM20 -= 60 * addHour;
                            sumH20 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH20 + "," + sumM20 + "</td>";
                        }
                        if (sumM21 >= 60)
                        {
                            addHour = sumM21 / 60;
                            sumM21 -= 60 * addHour;
                            sumH21 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH21 + "," + sumM21 + "</td>";
                        }
                        if (sumM22 >= 60)
                        {
                            addHour = sumM22 / 60;
                            sumM22 -= 60 * addHour;
                            sumH22 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH22 + "," + sumM22 + "</td>";
                        }
                        if (sumM23 >= 60)
                        {
                            addHour = sumM23 / 60;
                            sumM23 -= 60 * addHour;
                            sumH23 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH23 + "," + sumM23 + "</td></tr>";
                        }

                        strCode2 += strCode3;
                        sumH10 += sumH20; sumM10 += sumM20;
                        sumH11 += sumH21; sumM11 += sumM21;
                        sumH12 += sumH22; sumM12 += sumM22;
                        sumH13 += sumH23; sumM13 += sumM23;
                    }
                    
                }// End for code2

                if (sumM10 >= 60)
                {
                    addHour = sumM10 / 60;
                    sumM10 -= 60 * addHour;
                    sumH10 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH10 + "," + sumM10 + "</td>";
                }
                if (sumM11 >= 60)
                {
                    addHour = sumM11 / 60;
                    sumM11 -= 60 * addHour;
                    sumH11 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH11 + "," + sumM11 + "</td>";
                }
                if (sumM12 >= 60)
                {
                    addHour = sumM12 / 60;
                    sumM12 -= 60 * addHour;
                    sumH12 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH12 + "," + sumM12 + "</td>";
                }
                if (sumM13 >= 60)
                {
                    addHour = sumM13 / 60;
                    sumM13 -= 60 * addHour;
                    sumH13 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH13 + "," + sumM13 + "</td></tr>";
                }
                strCode1 += strCode2;
                sumH0 += sumH10; sumM0 += sumM10;
                sumH1 += sumH11; sumM1 += sumM11;
                sumH2 += sumH12; sumM2 += sumM12;
                sumH3 += sumH13; sumM3 += sumM13;
            }// End for code1

            result += "<tr><td style='border: 1px solid #D9E6F0;'>&nbsp</td><td style='border: 1px solid #D9E6F0;'><center><strong>Tổng số</strong></center></td><td style='border: 1px solid #D9E6F0;'>&nbsp</td>";

            // Thoi gian su dung
            if (sumM0 >= 60)
            {
                addHour = sumM0 / 60;
                sumM0 -= 60 * addHour;
                sumH0 += addHour;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH0 + "," + sumM0 + "</td>";
            // Gio tieu chuan
            if (sumM1 >= 60)
            {
                addHour = sumM1 / 60;
                sumM1 -= 60 * addHour;
                sumH1 += addHour;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH1 + "," + sumM1 + "</td>";
            // Ngoai gio
            if (sumM2 >= 60)
            {
                addHour = sumM2 / 60;
                sumM2 -= 60 * addHour;
                sumH2 += addHour;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH2 + "," + sumM2 + "</td>";
            // Ngay nghi
            if (sumM3 >= 60)
            {
                addHour = sumM3 / 60;
                sumM3 -= 60 * addHour;
                sumH3 += addHour;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH3 + "," + sumM3 + "</td></tr>";
            result += strCode1;
            result += "</table>";
            return result;
        }

        public static string repTimeCumulative(int month, int year, int code, bool isChild, string companyid)
        {
            string result = String.Empty;
            result += @"<table><tr>
			               <th align='center' style='color:#375B91;background-color:#F3F9FF;width:29px;border: 1px solid #D9E6F0;'>TT</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:240px;border: 1px solid #D9E6F0;'>Sản phẩm</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:60px;border: 1px solid #D9E6F0;'>Mã đơn vị/sản phẩm</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #D9E6F0;'>Thời gian làm việc trong tháng</th>
                           <th align='center' style='color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #D9E6F0;'>Thời gian sử dụng cộng dồn trong năm</th></tr>";

            DaReport report = new DaReport();
            DataTable listResultUnit = BuResultUnit.selAllResultUnit(companyid);

            // Dua project ra cac arraylist
            DataTable data = report.repTimeTotal(month, year, 1, companyid);
            DataTable code1 = data.Clone();
            DataTable code2 = data.Clone();
            DataTable code3;

            string strCode1 = String.Empty;
            string strCode2 = String.Empty;
            string strCode3 = String.Empty;
            int sumH0 = 0, sumM0 = 0, sumH1 = 0, sumM1 = 0, sumH10 = 0, sumM10 = 0, sumH11 = 0, sumM11 = 0, sumH20 = 0, sumM20 = 0, sumH21 = 0, sumM21 = 0;
            string resultTmp = String.Empty;

            string tmp;
            foreach (DataRow item in data.Rows)
            {
                // Loai bo dong co gia tri null
                if (!item["ProjectId"].ToString().Equals(""))
                {
                    tmp = item["ProjectLevel"].ToString();
                    if (tmp.Equals("1"))
                    {
                        code1.ImportRow(item);
                    }
                    if (tmp.Equals("2"))
                    {
                        code2.ImportRow(item);
                    }
                }
            }

            for (int i = 0; i < listResultUnit.Rows.Count; i++)
            {
                code3 = report.repTimeCumulative(month, year, Int32.Parse(listResultUnit.Rows[i]["ResultUnitID"].ToString()), companyid);
                for (int j = 0; j < code3.Rows.Count; j++)
                {
                    string tmpwe = code3.Rows[j]["ProjectName"].ToString();
                }

                strCode1 = "<tr><td align='center' style='border: 1px solid #D9E6F0;background-color:#D8DF43;'>" + (i + 1) + "</td><td style='border: 1px solid #D9E6F0;background-color:#D8DF43;'><strong>" + listResultUnit.Rows[i]["ResultUnitName"].ToString() + "</strong></td><td style='border: 1px solid #D9E6F0;background-color:#D8DF43;'>&nbsp;</td><td style='border: 1px solid #D9E6F0;background-color:#D8DF43;'>&nbsp;</td><td style='border: 1px solid #D9E6F0;background-color:#D8DF43;'>&nbsp;</td></tr>";
                #region
                    
                    // Luu tru dong code1, code 2, code 3
                    
                    

                    int hour, minute, addHour;

                    

                    foreach (DataRow item1 in code1.Rows)
                    {
                        string strTm1 = String.Empty;
                        if (code == 1)
                        {
                            strTm1 = "<tr><td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>&nbsp;</td><td style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'><strong>" + item1["ProjectName"].ToString() + "</strong></td><td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + item1["ProjectCode"].ToString() + "</td>";
                        }
                        strCode2 = String.Empty;
                        sumH10 = 0; sumM10 = 0; sumH11 = 0; sumM11 = 0;
                        foreach (DataRow item2 in code2.Rows)
                        {
                            if (item2["ProjectParentID"].ToString().Equals(item1["ProjectID"].ToString()))
                            {
                                string strTmp1 = String.Empty;
                                if (code == 2 || (code == 1 && isChild))
                                {
                                    strTmp1 = "<tr><td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>&nbsp;</td><td style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'><strong><i>" + item2["ProjectName"].ToString() + "</i></strong></td><td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + item2["ProjectCode"].ToString() + "</td>";
                                }

                                strCode3 = String.Empty;
                                sumH20 = 0; sumM20 = 0; sumH21 = 0; sumM21 = 0;
                                foreach (DataRow item3 in code3.Rows)
                                {

                                    if (item3["ProjectParentID"].ToString().Equals(item2["ProjectID"].ToString()))
                                    {
                                        // Thoi gian su dung
                                        hour = Int32.Parse(item3["FirstTimeH"].ToString());
                                        minute = Int32.Parse(item3["FirstTimeM"].ToString());
                                        sumH20 += hour;
                                        sumM20 += minute;

                                        if (minute >= 60)
                                        {
                                            addHour = minute / 60;
                                            minute -= 60 * addHour;
                                            hour += addHour;
                                        }
                                        string strValue1 = String.Empty;
                                        if (isChild || code == 3)
                                        {
                                            strValue1 = "<tr><td align='center' style='border: 1px solid #D9E6F0;'>&nbsp;</td><td style='border: 1px solid #D9E6F0;'>" + item3["ProjectName"].ToString() + "</td><td align='center' style='border: 1px solid #D9E6F0;'>" + item3["ProjectCode"].ToString() + "</td><td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td>";
                                        }
                                        // Gio tieu chuan
                                        hour = Int32.Parse(item3["SecondTimeH"].ToString());
                                        minute = Int32.Parse(item3["SecondTimeM"].ToString());
                                        sumH21 += hour;
                                        sumM21 += minute;

                                        if (minute >= 60)
                                        {
                                            addHour = minute / 60;
                                            minute -= 60 * addHour;
                                            hour += addHour;
                                        }
                                        string strValue2 = String.Empty;
                                        if (isChild || code == 3)
                                        {
                                            strValue2 = "<td align='center' style='border: 1px solid #D9E6F0;'>" + hour + "," + minute + "</td></tr>";
                                        }

                                        if (hour != 0 || minute != 0)
                                        {
                                            strCode3 += (strValue1 + strValue2);
                                        }
                                        
                                    }
                                }

                                if (sumM20 >= 60)
                                {
                                    addHour = sumM20 / 60;
                                    sumM20 -= 60 * addHour;
                                    sumH20 += addHour;
                                }
                                string strTmp2 = String.Empty;
                                if (code == 2 || (code == 1 && isChild))
                                {
                                    strTmp2 = "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH20 + "," + sumM20 + "</td>";
                                }
                                if (sumM21 >= 60)
                                {
                                    addHour = sumM21 / 60;
                                    sumM21 -= 60 * addHour;
                                    sumH21 += addHour;
                                }
                                string strTmp3 = String.Empty;
                                if (code == 2 || (code == 1 && isChild))
                                {
                                    strTmp3 = "<td align='center' style='border: 1px solid #D9E6F0;background-color:#F2F2F2;'>" + sumH21 + "," + sumM21 + "</td></tr>";
                                }
                                if (sumH21 != 0 || sumM21 != 0)
                                {
                                    strCode2 += (strTmp1 + strTmp2 + strTmp3);

                                }
                                

                                strCode2 += strCode3;
                                sumH10 += sumH20; sumM10 += sumM20;
                                sumH11 += sumH21; sumM11 += sumM21;
                            }

                        }// End for code2

                        if (sumM10 >= 60)
                        {
                            addHour = sumM10 / 60;
                            sumM10 -= 60 * addHour;
                            sumH10 += addHour;
                        }
                        string strTm2 = String.Empty;
                        if (code == 1)
                        {
                            strTm2 = "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH10 + "," + sumM10 + "</td>";
                        }
                        if (sumM11 >= 60)
                        {
                            addHour = sumM11 / 60;
                            sumM11 -= 60 * addHour;
                            sumH11 += addHour;
                        }
                        string strTm3 = String.Empty;
                        if (code == 1)
                        {
                            strTm3 = "<td align='center' style='border: 1px solid #D9E6F0;background-color:#C5C4C7;'>" + sumH11 + "," + sumM11 + "</td></tr>";
                        }
                        if (sumH11 != 0 || sumM11 != 0)
                        {
                            strCode1 += (strTm1 + strTm2 + strTm3);
                        }
                        
                        strCode1 += strCode2;
                        sumH0 += sumH10; sumM0 += sumM10;
                        sumH1 += sumH11; sumM1 += sumM11;

                    }// End for code1
                    
                #endregion

                    resultTmp += strCode1;
            }

            result += "<tr><td style='border: 1px solid #D9E6F0;'>&nbsp</td><td style='border: 1px solid #D9E6F0;'><center><strong>Tổng số</strong></center></td><td style='border: 1px solid #D9E6F0;'>&nbsp</td>";
            int addHour1;
            // Thoi gian su dung
            if (sumM0 >= 60)
            {
                addHour1 = sumM0 / 60;
                sumM0 -= 60 * addHour1;
                sumH0 += addHour1;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH0 + "," + sumM0 + "</td>";
            // Gio tieu chuan
            if (sumM1 >= 60)
            {
                addHour1 = sumM1 / 60;
                sumM1 -= 60 * addHour1;
                sumH1 += addHour1;
            }
            result += "<td align='center' style='border: 1px solid #D9E6F0;'>" + sumH1 + "," + sumM1 + "</td></tr>";
            result += resultTmp;
            result += "</table>";
            return result;
        }

        public static string repTimeTotalAdmin(int month, int year, bool id, int code, bool isChild, string companyid)
        {
            StringBuilder result = new StringBuilder();
            string label = id ? "tháng" : "năm";

            result.Append(@"<table cellspacing='0'><thead><tr>
			               <th rowspan='2' style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:29px;'>TT</th>
                           <th rowspan='2' style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:240px;'>Tên sản phẩm</th>
                           <th rowspan='2' style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:60px;'>Mã sản phẩm</th>
                           <th rowspan='2' style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;'>Thời gian sử dụng trong " + label + "</th>");
            result.Append(@"<th colspan='3' style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;'>Phân loại theo thời gian</th>
                        </tr>
                        <tr>
                           <th style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:110px;'>Giờ tiêu chuẩn</th>
                           <th style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:120px;'>Ngoài giờ</th>
                           <th style='border:1px solid #ccc;text-align:center;color:#375B91;background-color:#F3F9FF;width:90px;'>Ngày nghỉ(cuối tuần, lễ, tết)</th>
		                </tr></thead><tbody>");

            // Lay toan bo du lieu trong bao cao
            DaReport report = new DaReport();
            DataTable data;
            if (id)
            {
                data = report.repTimeTotal(month, year, 1, companyid);
            }
            else
            {
                data = report.repTimeTotal(month, year, 0, companyid);
            }

            // Dua project ra cac arraylist
            DataTable code1 = data.Clone();
            DataTable code2 = data.Clone();
            DataTable code3 = data.Clone();
            string tmp;

            // Luu tru tong gia tri dong tong so
            int sumH0 = 0, sumM0 = 0, sumH1 = 0, sumM1 = 0, sumH2 = 0, sumM2 = 0, sumH3 = 0, sumM3 = 0, sumH10 = 0, sumM10 = 0, sumH11 = 0, sumM11 = 0, sumH12 = 0, sumM12 = 0, sumH13 = 0, sumM13 = 0, sumH20 = 0, sumM20 = 0, sumH21 = 0, sumM21 = 0, sumH22 = 0, sumM22 = 0, sumH23 = 0, sumM23 = 0;

            // Luu tru dong code1, code 2, code 3
            string strCode1 = String.Empty;
            string strCode2 = String.Empty;
            string strCode3 = String.Empty;
            foreach (DataRow item in data.Rows)
            {
                // Loai bo dong co gia tri null
                if (!item["ProjectId"].ToString().Equals(""))
                {
                    tmp = item["ProjectLevel"].ToString();
                    if (tmp.Equals("1"))
                    {
                        code1.ImportRow(item);
                    }
                    if (tmp.Equals("2"))
                    {
                        code2.ImportRow(item);
                    }
                    if (tmp.Equals("3"))
                    {
                        code3.ImportRow(item);
                    }
                }
            }

            int hour, minute, addHour;

            int count = 1;

            foreach (DataRow item1 in code1.Rows)
            {
                if (code == 1)
                {
                    strCode1 += "<tr><td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + (count++) + "</td><td style='border:1px solid #ccc;background-color:#FFC;'><strong>" + item1["ProjectName"].ToString() + "</strong></td><td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + item1["ProjectCode"].ToString() + "</td>";
                }
                strCode2 = String.Empty;
                sumH10 = 0; sumM10 = 0; sumH11 = 0; sumM11 = 0; sumH12 = 0; sumM12 = 0; sumH13 = 0; sumM13 = 0;
                foreach (DataRow item2 in code2.Rows)
                {
                    if (item2["ProjectParentID"].ToString().Equals(item1["ProjectID"].ToString()))
                    {
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<tr><td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + (count++) + "</td><td style='border:1px solid #ccc;background-color:#F2F2F2;'><strong><i>" + item2["ProjectName"].ToString() + "</i></strong></td><td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + item2["ProjectCode"].ToString() + "</td>";
                        }

                        strCode3 = String.Empty;
                        sumH20 = 0; sumM20 = 0; sumH21 = 0; sumM21 = 0; sumH22 = 0; sumM22 = 0; sumH23 = 0; sumM23 = 0;
                        foreach (DataRow item3 in code3.Rows)
                        {

                            if (item3["ProjectParentID"].ToString().Equals(item2["ProjectID"].ToString()))
                            {
                                // Thoi gian su dung
                                hour = Int32.Parse(item3["TimeHour"].ToString());
                                minute = Int32.Parse(item3["TimeMinute"].ToString());
                                sumH20 += hour;
                                sumM20 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<tr><td style='border:1px solid #ccc;text-align:center;'>" + (count++) + "</td><td style='border:1px solid #ccc;'>" + item3["ProjectName"].ToString() + "</td><td style='border:1px solid #ccc;text-align:center;'>" + item3["ProjectCode"].ToString() + "</td><td style='border:1px solid #ccc;text-align:center;'>" + hour + "," + minute + "</td>";
                                }
                                // Gio tieu chuan
                                hour = Int32.Parse(item3["FirstTimeH"].ToString());
                                minute = Int32.Parse(item3["FirstTimeM"].ToString());
                                sumH21 += hour;
                                sumM21 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td style='border:1px solid #ccc;text-align:center;'>" + hour + "," + minute + "</td>";
                                }
                                // Ngoai gio
                                hour = Int32.Parse(item3["SecondTimeH"].ToString());
                                minute = Int32.Parse(item3["SecondTimeM"].ToString());
                                sumH22 += hour;
                                sumM22 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td style='border:1px solid #ccc;text-align:center;'>" + hour + "," + minute + "</td>";
                                }
                                // Ngay nghi
                                hour = Int32.Parse(item3["ThirdTimeH"].ToString());
                                minute = Int32.Parse(item3["ThirdTimeM"].ToString());
                                sumH23 += hour;
                                sumM23 += minute;

                                if (minute >= 60)
                                {
                                    addHour = minute / 60;
                                    minute -= 60 * addHour;
                                    hour += addHour;
                                }
                                if (isChild || code == 3)
                                {
                                    strCode3 += "<td style='border:1px solid #ccc;text-align:center;'>" + hour + "," + minute + "</td></tr>";
                                }
                            }
                        }

                        if (sumM20 >= 60)
                        {
                            addHour = sumM20 / 60;
                            sumM20 -= 60 * addHour;
                            sumH20 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + sumH20 + "," + sumM20 + "</td>";
                        }
                        if (sumM21 >= 60)
                        {
                            addHour = sumM21 / 60;
                            sumM21 -= 60 * addHour;
                            sumH21 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + sumH21 + "," + sumM21 + "</td>";
                        }
                        if (sumM22 >= 60)
                        {
                            addHour = sumM22 / 60;
                            sumM22 -= 60 * addHour;
                            sumH22 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + sumH22 + "," + sumM22 + "</td>";
                        }
                        if (sumM23 >= 60)
                        {
                            addHour = sumM23 / 60;
                            sumM23 -= 60 * addHour;
                            sumH23 += addHour;
                        }
                        if (code == 2 || (code == 1 && isChild))
                        {
                            strCode2 += "<td style='border:1px solid #ccc;text-align:center;background-color:#F2F2F2;'>" + sumH23 + "," + sumM23 + "</td></tr>";
                        }

                        strCode2 += strCode3;
                        sumH10 += sumH20; sumM10 += sumM20;
                        sumH11 += sumH21; sumM11 += sumM21;
                        sumH12 += sumH22; sumM12 += sumM22;
                        sumH13 += sumH23; sumM13 += sumM23;
                    }

                }// End for code2

                if (sumM10 >= 60)
                {
                    addHour = sumM10 / 60;
                    sumM10 -= 60 * addHour;
                    sumH10 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + sumH10 + "," + sumM10 + "</td>";
                }
                if (sumM11 >= 60)
                {
                    addHour = sumM11 / 60;
                    sumM11 -= 60 * addHour;
                    sumH11 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + sumH11 + "," + sumM11 + "</td>";
                }
                if (sumM12 >= 60)
                {
                    addHour = sumM12 / 60;
                    sumM12 -= 60 * addHour;
                    sumH12 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + sumH12 + "," + sumM12 + "</td>";
                }
                if (sumM13 >= 60)
                {
                    addHour = sumM13 / 60;
                    sumM13 -= 60 * addHour;
                    sumH13 += addHour;
                }
                if (code == 1)
                {
                    strCode1 += "<td style='border:1px solid #ccc;text-align:center;background-color:#FFC;'>" + sumH13 + "," + sumM13 + "</td></tr>";
                }
                strCode1 += strCode2;
                sumH0 += sumH10; sumM0 += sumM10;
                sumH1 += sumH11; sumM1 += sumM11;
                sumH2 += sumH12; sumM2 += sumM12;
                sumH3 += sumH13; sumM3 += sumM13;
            }// End for code1

            result.Append("<tr><td style='border:1px solid #ccc;'>&nbsp</td><td style='border:1px solid #ccc;'><center><strong>Tổng số</strong></center></td><td style='border:1px solid #ccc;'>&nbsp</td>");

            // Thoi gian su dung
            if (sumM0 >= 60)
            {
                addHour = sumM0 / 60;
                sumM0 -= 60 * addHour;
                sumH0 += addHour;
            }
            result.Append("<td style='border:1px solid #ccc;text-align:center;'>" + sumH0 + "," + sumM0 + "</td>");
            // Gio tieu chuan
            if (sumM1 >= 60)
            {
                addHour = sumM1 / 60;
                sumM1 -= 60 * addHour;
                sumH1 += addHour;
            }
            result.Append("<td style='border:1px solid #ccc;text-align:center;'>" + sumH1 + "," + sumM1 + "</td>");
            // Ngoai gio
            if (sumM2 >= 60)
            {
                addHour = sumM2 / 60;
                sumM2 -= 60 * addHour;
                sumH2 += addHour;
            }
            result.Append("<td style='border:1px solid #ccc;text-align:center;'>" + sumH2 + "," + sumM2 + "</td>");
            // Ngay nghi
            if (sumM3 >= 60)
            {
                addHour = sumM3 / 60;
                sumM3 -= 60 * addHour;
                sumH3 += addHour;
            }
            result.Append("<td style='border:1px solid #ccc;text-align:center;'>" + sumH3 + "," + sumM3 + "</td></tr>");
            result.Append(strCode1);
            result.Append("</tbody></table>");
            return result.ToString();
        }

        public static string repTimeCumulativeAdmin(int month, int year, int code, bool isChild, string companyid)
        {
            string result = String.Empty;
            result += @"<table cellspacing='0'><thead><tr>
			               <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:29px;border: 1px solid #CCC;'>TT</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:240px;border: 1px solid #CCC;'>Sản phẩm</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:60px;border: 1px solid #CCC;'>Mã đơn vị/sản phẩm</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #CCC;'>Thời gian làm việc trong tháng</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #CCC;'>Thời gian sử dụng cộng dồn trong năm</th></tr></thead><tbody>";

            DaReport report = new DaReport();
            DataTable listResultUnit = BuResultUnit.selAllResultUnit(companyid);

            // Dua project ra cac arraylist
            DataTable data = report.repTimeTotal(month, year, 1, companyid);
            DataTable code1 = data.Clone();
            DataTable code2 = data.Clone();
            DataTable code3;

            string strCode1 = String.Empty;
            string strCode2 = String.Empty;
            string strCode3 = String.Empty;
            int sumH0 = 0, sumM0 = 0, sumH1 = 0, sumM1 = 0, sumH10 = 0, sumM10 = 0, sumH11 = 0, sumM11 = 0, sumH20 = 0, sumM20 = 0, sumH21 = 0, sumM21 = 0;
            string resultTmp = String.Empty;

            string tmp;
            foreach (DataRow item in data.Rows)
            {
                // Loai bo dong co gia tri null
                if (!item["ProjectId"].ToString().Equals(""))
                {
                    tmp = item["ProjectLevel"].ToString();
                    if (tmp.Equals("1"))
                    {
                        code1.ImportRow(item);
                    }
                    if (tmp.Equals("2"))
                    {
                        code2.ImportRow(item);
                    }
                }
            }

            for (int i = 0; i < listResultUnit.Rows.Count; i++)
            {
                code3 = report.repTimeCumulative(month, year, Int32.Parse(listResultUnit.Rows[i]["ResultUnitID"].ToString()), companyid);
                for (int j = 0; j < code3.Rows.Count; j++)
                {
                    string tmpwe = code3.Rows[j]["ProjectName"].ToString();
                }

                strCode1 = "<tr><td style='text-align:center;border: 1px solid #CCC;background-color:#FFC;'>" + (i + 1) + "</td><td style='border: 1px solid #CCC;background-color:#FFC;'><strong>" + listResultUnit.Rows[i]["ResultUnitName"].ToString() + "</strong></td><td style='border: 1px solid #CCC;background-color:#FFC;'>&nbsp;</td><td style='border: 1px solid #CCC;background-color:#FFC;'>&nbsp;</td><td style='border: 1px solid #CCC;background-color:#FFC;'>&nbsp;</td></tr>";
                #region

                // Luu tru dong code1, code 2, code 3

                int hour, minute, addHour;

                foreach (DataRow item1 in code1.Rows)
                {
                    string strTm1 = String.Empty;
                    if (code == 1)
                    {
                        strTm1 = "<tr><td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>&nbsp;</td><td style='border: 1px solid #CCC;background-color:#F2F2F2;'><strong>" + item1["ProjectName"].ToString() + "</strong></td><td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + item1["ProjectCode"].ToString() + "</td>";
                    }
                    strCode2 = String.Empty;
                    sumH10 = 0; sumM10 = 0; sumH11 = 0; sumM11 = 0;
                    foreach (DataRow item2 in code2.Rows)
                    {
                        if (item2["ProjectParentID"].ToString().Equals(item1["ProjectID"].ToString()))
                        {
                            string strTmp1 = String.Empty;
                            if (code == 2 || (code == 1 && isChild))
                            {
                                strTmp1 = "<tr><td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>&nbsp;</td><td style='border: 1px solid #CCC;background-color:#F2F2F2;'><strong><i>" + item2["ProjectName"].ToString() + "</i></strong></td><td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + item2["ProjectCode"].ToString() + "</td>";
                            }

                            strCode3 = String.Empty;
                            sumH20 = 0; sumM20 = 0; sumH21 = 0; sumM21 = 0;
                            foreach (DataRow item3 in code3.Rows)
                            {

                                if (item3["ProjectParentID"].ToString().Equals(item2["ProjectID"].ToString()))
                                {
                                    // Thoi gian su dung
                                    hour = Int32.Parse(item3["FirstTimeH"].ToString());
                                    minute = Int32.Parse(item3["FirstTimeM"].ToString());
                                    sumH20 += hour;
                                    sumM20 += minute;

                                    if (minute >= 60)
                                    {
                                        addHour = minute / 60;
                                        minute -= 60 * addHour;
                                        hour += addHour;
                                    }
                                    string strValue1 = String.Empty;
                                    if (isChild || code == 3)
                                    {
                                        strValue1 = "<tr><td style='text-align:center;border: 1px solid #CCC;'>&nbsp;</td><td style='border: 1px solid #CCC;'>" + item3["ProjectName"].ToString() + "</td><td style='text-align:center;border: 1px solid #CCC;'>" + item3["ProjectCode"].ToString() + "</td><td style='text-align:center;border: 1px solid #CCC;'>" + hour + "," + minute + "</td>";
                                    }
                                    // Gio tieu chuan
                                    hour = Int32.Parse(item3["SecondTimeH"].ToString());
                                    minute = Int32.Parse(item3["SecondTimeM"].ToString());
                                    sumH21 += hour;
                                    sumM21 += minute;

                                    if (minute >= 60)
                                    {
                                        addHour = minute / 60;
                                        minute -= 60 * addHour;
                                        hour += addHour;
                                    }
                                    string strValue2 = String.Empty;
                                    if (isChild || code == 3)
                                    {
                                        strValue2 = "<td style='text-align:center;border: 1px solid #CCC;'>" + hour + "," + minute + "</td></tr>";
                                    }

                                    if (hour != 0 || minute != 0)
                                    {
                                        strCode3 += (strValue1 + strValue2);
                                    }

                                }
                            }

                            if (sumM20 >= 60)
                            {
                                addHour = sumM20 / 60;
                                sumM20 -= 60 * addHour;
                                sumH20 += addHour;
                            }
                            string strTmp2 = String.Empty;
                            if (code == 2 || (code == 1 && isChild))
                            {
                                strTmp2 = "<td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + sumH20 + "," + sumM20 + "</td>";
                            }
                            if (sumM21 >= 60)
                            {
                                addHour = sumM21 / 60;
                                sumM21 -= 60 * addHour;
                                sumH21 += addHour;
                            }
                            string strTmp3 = String.Empty;
                            if (code == 2 || (code == 1 && isChild))
                            {
                                strTmp3 = "<td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + sumH21 + "," + sumM21 + "</td></tr>";
                            }
                            if (sumH21 != 0 || sumM21 != 0)
                            {
                                strCode2 += (strTmp1 + strTmp2 + strTmp3);

                            }


                            strCode2 += strCode3;
                            sumH10 += sumH20; sumM10 += sumM20;
                            sumH11 += sumH21; sumM11 += sumM21;
                        }

                    }// End for code2

                    if (sumM10 >= 60)
                    {
                        addHour = sumM10 / 60;
                        sumM10 -= 60 * addHour;
                        sumH10 += addHour;
                    }
                    string strTm2 = String.Empty;
                    if (code == 1)
                    {
                        strTm2 = "<td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + sumH10 + "," + sumM10 + "</td>";
                    }
                    if (sumM11 >= 60)
                    {
                        addHour = sumM11 / 60;
                        sumM11 -= 60 * addHour;
                        sumH11 += addHour;
                    }
                    string strTm3 = String.Empty;
                    if (code == 1)
                    {
                        strTm3 = "<td style='text-align:center;border: 1px solid #CCC;background-color:#F2F2F2;'>" + sumH11 + "," + sumM11 + "</td></tr>";
                    }
                    if (sumH11 != 0 || sumM11 != 0)
                    {
                        strCode1 += (strTm1 + strTm2 + strTm3);
                    }

                    strCode1 += strCode2;
                    sumH0 += sumH10; sumM0 += sumM10;
                    sumH1 += sumH11; sumM1 += sumM11;

                }// End for code1

                #endregion

                resultTmp += strCode1;
            }

            result += "<tr><td style='border: 1px solid #CCC;'>&nbsp</td><td style='border: 1px solid #CCC;'><center><strong>Tổng số</strong></center></td><td style='border: 1px solid #CCC;'>&nbsp</td>";
            int addHour1;
            // Thoi gian su dung
            if (sumM0 >= 60)
            {
                addHour1 = sumM0 / 60;
                sumM0 -= 60 * addHour1;
                sumH0 += addHour1;
            }
            result += "<td style='text-align:center;border: 1px solid #CCC;'>" + sumH0 + "," + sumM0 + "</td>";
            // Gio tieu chuan
            if (sumM1 >= 60)
            {
                addHour1 = sumM1 / 60;
                sumM1 -= 60 * addHour1;
                sumH1 += addHour1;
            }
            result += "<td style='text-align:center;border: 1px solid #CCC;'>" + sumH1 + "," + sumM1 + "</td></tr>";
            result += resultTmp;
            result += "</tbody></table>";
            return result;
        }
    }
}
